//<script type='text/javascript'>

	////////////////////////////////////////// EXTENTION: BAR: CALENDAR
var Calendar = function ( vars ) {
	
	Object.extend(obj = new Element("div", {id:"calendar", class:"ext calendar"}), dashboard.ext.calendar );
	
	//// BUILD grid UNIT
	obj.insert( obj.grid = new Unit( "connecting" ));
	obj.grid.descr.insert({ before: new Element("div", {id:"cal.arrr", class:"arright"}).observe("click", obj.requestFlip.bind( obj )) });
	obj.grid.descr.insert({ before: new Element("div", {id:"cal.arrl", class:"arrleft"}).observe("click", obj.requestFlip.bind( obj )) });
	
	$R(0, 5).each( function( n ) {
		obj.grid.content.insert( li = new Element( "li" ));
		$R(0, 6).each( function( m ) { li.insert(new Element("span", { id: "cal.d" + ( n*7+m )})); });
	});
	
	obj.insert( obj.content = new Element( "div" ));
	
	return obj.request( vars || {});

}


dashboard.ext.calendar	= {
	selected	: 0,
	addbutton	: function() {
		obj = new Element("div", { id:"dashboard.ext.calbutton", class:"ext_button cal", name:"ext:calendar"}).observe('click', dashboard.showhide);
		obj.insert( new Element("div", { class:"month" }).update("<?=date("M")?>") ).ext = Calendar;
		return obj.insert( dashboard.date.getDate() );
	},
	addlist		: function( descr, seed, columns, plus ) {
	
		this.insert( this.field = new Unit( descr, "ul", columns ));
		if( plus && false )
			this.field.content.insert( new Element( "li", { class: "plus" }).observe( "click", plus ))
		
	},
	requestFlip		: function( e ) {

		this.grid.descr.update("loading...");
		this.request({ vars:{ hook:this.params.vars.hook + (( e.currentTarget.id=="cal.arrl")? -1: 1)} });
	},
	
	request		: function( params ) {

		Object.extend(this.params = { trg:this.id, type:'cal', transfer_type:"push", action:"seed", vars:{ hook:0 }}, params);
		xhttp.send('control.call.php', this.params);
		return this;
	},
	
	seed		: function(dates, vars) {
		day = new Date(vars.year, vars.month, vars.day);
		today = Math.floor( new Date().getTime()/24/60/60/1000 );
		
		this.dates = (dates)? dates : {};
		
		$R(0, 41).each(function(n) {
			
			node = $('cal.d'+n).update(day.getDate()).observe("click", this.optionDay.bind( this ));
			node.uts = day.getTime()/1000;
			node.className = "";
			
			if( node.uts == this.selected ) 			node.addClassName( "select" );
			if( day.getMonth() != vars.month ) 			node.addClassName( "blur" );
			if( Math.ceil(node.uts/24/60/60) == today )	node.addClassName( "now" );
			if( this.dates[ String( node.uts )])		node.addClassName( "active" );

  			day = new Date( day.getFullYear(), day.getMonth(), day.getDate()+1 );
		}, this );
		
		obj.grid.descr.update( vars.monthstring );
	},
	
	day	: function(dates, vars) {
		$("cal.field").update( new Element("h1").update(vars.date) );		
	},
	
	expandField	: function( vars ) {
		
		this.objhook = vars;
		
		this.field.update( new Element( "h1" ).update( vars.objtype + " &lsquo;" + vars.name + "&rsquo;" ) );
		this.field.insert( new Element( "span", { class:"descr" }).update( "overview:" ));
		
		this.field.insert( this.tags = new Element( "div", { class:"tags" }).update( "Click in calendar to add date." ));
	},
	
	tagseed		: function( dates ) {
	
		this.tags.update();
	
		dates.each( function( dte ){
			
			this.tags.insert( day = new Element( "span", { class:"tag" }).update( dte.descr ).observe( "click", this.editDay.bind( this )));
			day.vars = dte;
			this.select( "td.active" ).each( function( td ){ if( td.uts == dte.day ) td.addClassName( "select" )});
		}, 	this );
	},
	
	newDay	: function( uts, obj ) {
		
		tooltip.remove();
		this.content.update( this.info = new Info( "loading." ));
		vars = { type: "cal.single", uts: uts, objid: ( obj.id )? obj.id : 0, objtype: ( obj.type )? obj.type : false };
		
		console.log( vars )
		
		xhttp.send( 'control.call.php', { trg: this.id, type:'cal.single', transfer_type:"push", action:"cal_new", vars: vars });
	},
	
	editDay		: function( e, create ) {
		
		tooltip.remove();
		this.content.update( this.info = new Info( "loading." ));
		
		day = e.currentTarget;
		vars = { id:day.vars.ID, type: engine.type_r[ day.vars.type ].key };
		xhttp.send('control.call.php', { trg:"calendar", type: vars.type, action:"cal_edit", vars:vars });
	},
	
	optionDay	: function( e ) {
		
		el = e.currentTarget;
		dates = this.dates[ String( el.uts )];
		
		content = [ el.innerHTML, new Element( "span", { class:"new" }).update( "new" ).observe( "click", this.newDay.bind( this, el.uts )) ];
		
		if( dates )
			dates.each( function( node ){ content.push( time = new Element( "span", { class:"time" }).insert( node.time ).observe( "click", this.editDay.bind( this ))); time.vars = node; }, this );
			
		if( this.objhook )
			content.push( new Element( "span", { class:"new" }).update( "add the " + this.objhook.type ).observe( "click", this.newDay.bind( this, el.uts, this.objhook )));


		document.body.insert( new Tooltip( el, content, "line", true ));
	},
	
	dayseed		: function( descr, content, column ) {
		
		this.info.descr.update( descr );
		
		if( content[0] ) content.each( function( el ){ this.info.content.insert( new Element( "li" ).update( el )) }, this );
		else this.info.content.insert( content );
		
		if( column )
			column.each( function( el ){ this.info.column.insert( el ); }, this );
	},
	
	calcspan	: function( e ) {
		obj		=  e.currentTarget;
		
        start	= dashboard.ext.calendar.sqldate( this.value );
        end		= dashboard.ext.calendar.sqldate( obj.value );
		calc 	= Math.round(( end.getTime() - start.getTime() ) / 60000 );
		
		return ( calc > 0 )? calc : 0;
	},
	
	sqldate	: function( string ) {
		dd	= string.split( " " );
        ar	= dd[0].split( "-" );
        tr	= dd[1].split( ":" );
        
		return new Date( ar[0], ar[1], ar[2], tr[0], tr[1], tr[2] );
	},
	
	changetype	: function() {
		dashboard.comment( "changing type" );
	}
}
